package com.itdct.onflow.system.common.websocket

import org.springframework.beans.factory.annotation.Autowired
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty
import org.springframework.context.annotation.Configuration
import org.springframework.web.socket.config.annotation.EnableWebSocket
import org.springframework.web.socket.config.annotation.WebSocketConfigurer
import org.springframework.web.socket.config.annotation.WebSocketHandlerRegistry

/**
 * @author Zhouwx
 * @date 2025/2/24 17:21:24
 * @version 1.0
 * @description
 */
@ConditionalOnProperty(name = ["onflow.websocket.enable"], havingValue = "true")
@Configuration
@EnableWebSocket
class WebSocketConfig : WebSocketConfigurer {

    @Autowired
    private lateinit var webSocketHandler: SysWebSocketHandler

    @Autowired
    private lateinit var handshakeInterceptor: AuthHandshakeInterceptor

    override fun registerWebSocketHandlers(registry: WebSocketHandlerRegistry) {
        registry.addHandler(webSocketHandler, "/ws")
            .addInterceptors(handshakeInterceptor)
            .setAllowedOrigins("*") // 生产环境应配置具体域名
    }
}